//
//  HFRow.m
//  ThridPackage
//
//  Created by 韩艳锋 on 2019/7/23.
//  Copyright © 2019 韩艳锋. All rights reserved.
//

#import "HFRow.h"

@interface HFRow ()

@property (assign, nonatomic) Class cellClass;

@end

@implementation HFRow

@synthesize interface = _interface;
@synthesize other = _other;

- (instancetype)initWithCellClass:(Class)cellClass interface:(id _Nullable)interface {
    self = [super init];
    if (self) {
        self.hasCellTipBack = YES;
        _interface = interface;
        self.cellClass = cellClass;
    }
    return self;
}

- (instancetype)initWithCellClass:(Class)cellClass interface:(id _Nullable)interface other:(id _Nullable)other {
    self = [self initWithCellClass:cellClass interface:interface];
    if (self) {
        _other = other;
    }
    return self;
}

- (NSString*)reuseIdentifier {
    return [NSString stringWithFormat:@"%@", self.cellClass];
}

- (UITableViewCell<HFCellUpdatable>* _Nonnull)cellWithTableView:(UITableView*)tableView {
    UITableViewCell<HFCellUpdatable>* _Nonnull cell = [tableView dequeueReusableCellWithIdentifier:self.reuseIdentifier];
    if (!cell) {
        cell = [self.cellClass alloc];
        cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.reuseIdentifier];
    }
    if ([cell respondsToSelector:@selector(updateViewData:)]) {
        [cell updateViewData:self];
    }
    return cell;
}

- (UICollectionViewCell<HFCellUpdatable>* _Nonnull)cellWithCollectionView:(UICollectionView*)collectionView indexPath:(NSIndexPath*)indexPath {
    UICollectionViewCell<HFCellUpdatable>* cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.reuseIdentifier forIndexPath:indexPath];
    if ([cell respondsToSelector:@selector(updateViewData:)]) {
        [cell updateViewData:self];
    }
    return cell;
}

- (void)cellActionWith:(NSInteger)tag value:(id _Nullable)value {
    if (tag == -1 && !self.hasCellTipBack) {
        return;
    }
    if ([self.delegate respondsToSelector:@selector(actionFromCellRow:tag:value:)]) {
        [self.delegate actionFromCellRow:self tag:tag value: value];
    }
}

- (void)refreshCell {
    if ([self.cell respondsToSelector:@selector(updateViewData:)]) {
        [self.cell updateViewData:self];
    }
}
@end
